package com.laterem.rtc.web;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laterem.rtc.domain.ActivityDO;
import com.laterem.rtc.domain.ActivityItemDO;
import com.laterem.rtc.domain.UserDO;
import com.laterem.rtc.service.UserService;
import com.laterem.rtc.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @creator: alan wu
 * @dateTime: 2023/5/28 16:40
 * @desc:
 */
@Tag(name = "答题接口", description = "答题接口")
@RequestMapping("/answer")
@RestController
public class AnswerController {

    @Autowired
    private UserService userService;

    private static Map<String, String> loginCache = new ConcurrentHashMap<>();

//    static {
//        loginCache.put("wu", "oTw3S5KL06Yd6UmT8EvlzQ1sLItE");
//    }

    @Operation(summary = "普通用户登录")
    @PostMapping("/wechat/login")
    public Result<Object> wechatLogin(@RequestBody UserDO userDO) {
        String data = userService.wechatLogin(userDO);
        String key = UUID.randomUUID().toString();
        putCache(key, data);
        return Result.ok(key);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/wechat/user/info")
    public Result<Object> userInfo(HttpServletRequest request) {
        String openId = loginCache.get(request.getHeader("laterem"));
        UserDO userDO = null;
        if (StringUtils.isNotBlank(openId)){
            userDO = userService.getUserInfo(openId);
        }
        return Result.ok(userDO);
    }



    @Operation(summary = "用户更新")
    @PostMapping("/wechat/update/user")
    public Result<Object> updateUser(@RequestBody UserDO userDO) {
        userService.updateUser(userDO);
        return Result.ok();
    }

    @Operation(summary = "获取openId")
    @PostMapping("/wechat/openId")
    public Result<Object> getOpenId(@RequestParam("code") String code) {
        String data = userService.getOpenId(code);
        return Result.ok(data);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/admin/user/info")
    public Result<Object> userInfo(@RequestParam("openId") String openId) {
        UserDO userDO = null;
        if (StringUtils.isNotBlank(openId)){
            userDO = userService.getUserInfo(openId);
        }
        return Result.ok(userDO);
    }

    @Operation(summary = "管理员登录")
    @GetMapping("/admin/login")
    public Result<Object> adminLogin(@RequestParam("username")String username, @RequestParam("password")String password) {
        String data = userService.adminLogin(username, password);
        if (StringUtils.isBlank(data)){
            return Result.fail();
        }
        return Result.ok(data);
    }

    @Operation(summary = "查询用户列表")
    @GetMapping("/admin/list")
    public Result<Object> userList(Page page) {
        IPage<UserDO> data = userService.userList(page);
        return Result.ok(data);
    }

    @Operation(summary = "创建场次")
    @PostMapping("/admin/activity")
    public Result<Object> saveActivity(@RequestBody ActivityDO activityDO) {
        userService.saveActivity(activityDO);
        return Result.ok();
    }

    @Operation(summary = "更新场次")
    @PutMapping("/admin/activity")
    public Result<Object> updateActivity(@RequestBody ActivityDO activityDO) {
        userService.updateActivity(activityDO);
        return Result.ok();
    }

    @Operation(summary = "删除场次")
    @DeleteMapping("/admin/activity")
    public Result<Object> deleteActivity(@RequestParam("id") Integer id) {
        userService.deleteActivity(id);
        return Result.ok();
    }

    @Operation(summary = "场次列表")
    @GetMapping("/admin/activity")
    public Result<Object> activityList(Page page) {
        IPage<ActivityDO> data = userService.activityList(page);
        return Result.ok(data);
    }

    @Operation(summary = "创建场次轮次")
    @PostMapping("/admin/activityItem")
    public Result<Object> saveActivityItem(@RequestBody ActivityItemDO activityItemDO) {
        userService.saveActivityItem(activityItemDO);
        return Result.ok();
    }

    @Operation(summary = "更新场次轮次")
    @PutMapping("/admin/activityItem")
    public Result<Object> updateActivityItem(@RequestBody ActivityItemDO activityItemDO) {
        userService.updateActivityItem(activityItemDO);
        return Result.ok();
    }

    @Operation(summary = "删除场次轮次")
    @DeleteMapping("/admin/activityItem")
    public Result<Object> deleteActivityItem(@RequestParam("id") Integer id) {
        userService.deleteActivityItem(id);
        return Result.ok();
    }

    @Operation(summary = "获取最新的轮次")
    @GetMapping("/wechat/new/activityItem")
    public Result<Object> newActivityItem(@RequestHeader("laterem") String openId) {
        String openIdStr = getOpenIdStr(openId);
        ActivityItemDO activityItemDO = userService.newActivityItem(openIdStr);
        return Result.ok(activityItemDO);
    }

    @Operation(summary = "下注轮次")
    @GetMapping("/wechat/bet")
    public Result<Object> bet(@RequestParam("activityItemId") Integer activityItemId,
                              @RequestParam("guessResult") Integer guessResult,
                              @RequestHeader("laterem") String openId) {
        String openIdStr = getOpenIdStr(openId);
        userService.bet(activityItemId, guessResult, openIdStr);
        return Result.ok();
    }

    @Operation(summary = "查询最近场次信息")
    @GetMapping("/wechat/activity")
    public Result<Object> activity(@RequestHeader("laterem") String openId) {
        String openIdStr = getOpenIdStr(openId);
        ActivityDO activityDO = userService.activity(openIdStr);
        return Result.ok(activityDO);
    }

    @Operation(summary = "设置奖池")
    @PutMapping("/admin/jackpot")
    public Result<Object> updateJackpot(@RequestParam("number") Integer number) {
        userService.updateJackpot(number);
        return Result.ok();
    }

    @Operation(summary = "查询奖池")
    @GetMapping("/admin/jackpot")
    public Result<Object> selectAdminJackpot() {
        String labelValue = userService.selectJackpot();
        return Result.ok(Integer.parseInt(labelValue));
    }

    @Operation(summary = "查询奖池")
    @GetMapping("/wechat/jackpot")
    public Result<Object> selectJackpot() {
        String labelValue = userService.selectJackpot();
        return Result.ok(Integer.parseInt(labelValue));
    }

    private void putCache(String key, String openId){
        loginCache.put(key, openId);
    }

    private String getOpenIdStr(String key){
        String openId = loginCache.get(key);
        if (StringUtils.isNotBlank(openId)){
            return openId;
        } else {
            throw new RuntimeException("登录过期");
        }
    }
}
